#include "..\head\game.h"

// 1.初始化棋盘

static void Init_Board (char Board [ROWS] [COLS] , int rows , int cols , char set)
{

    for (int j = 0 ; j < rows ; j++)
    {

        for (int k = 0 ; k < cols ; k++ )
        {

            Board [j] [k] = set ;

        }

    }

}

// 2.测试打印棋盘

static void Display_Board (char Board [ROWS] [COLS] , int row , int col)
{

    for (int i = 0 ; i <= col ; i++)
    {

        printf("%d " , i) ;

    }

    printf("\n") ;

    for(int j = 1 ; j <= row ; j++)
    {

        printf("%d " , j) ;

        for (int k = 1 ; k <= col ; k++)
        {

            printf("%c " , Board [j] [k]) ;

        }

        printf("\n") ;

    }

    printf("\n") ;

}

// 3.向棋盘里面布置雷

static void Set_Mine (char Board [ROWS] [COLS] , int row , int col , int count)
{

    int x ;
    int y ;

    while (count)
    {

        x = rand () % row + 1 ;

        y = rand () % col + 1 ;

        if (Board [x] [y] == '0')
        {

            Board [x] [y] = '1' ;

            count-- ;

        }

    }

}

// 找有多少雷

static int Get_Mine_Count (char mine [ROWS] [COLS] , int x , int y)
{

    int get_count ;

    for (int j = y - 1 ; j <= y + 1 ; j++)
    {

        for (int k = x - 1 ; k <= x + 1 ; k++)
        {

            get_count += mine [j] [k] ;

        }

    }

    return get_count - (9 * '0') ;

}


// 4.扫雷

static void Find_Mine (char mine [ROWS] [COLS] , char show [ROWS] [COLS] , int row , int col , int count)
{

    int x = 0 , y = 0 , win = 0 ;

    Display_Board (show , row , col) ;

    while (win < (row * col) - count)
    {


        printf("please choice the location in the Board (x y):") ;
        scanf("%d %d" , &x , &y) ;

        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {

            if (mine [x] [y] == '1')
            {

                system("cls") ;

                printf("you choice the mine , gameover!\n") ;

                Display_Board (mine , row , col) ;

                Sleep(5000) ;

                system("cls") ;

                break ;

            }

            else
            {

                // 这个坐标是否被排查过

                if (show [x] [y] == '*')
                {

                    // 如果该位置不是雷,就统计周围坐标几个雷

                    int get_count = Get_Mine_Count (mine , x , y) ;

                    show [x] [y] = get_count + '0' ;

                    system("cls") ;

                    Display_Board(show , row , col) ;

                    win++ ;

                }

                else
                {

                    system("cls") ;

                    printf("this location had been check , please choice again.\n") ;

                    Display_Board(show , row , col) ;

                }

            }

        }
        
        else
        {

            system("cls") ;

            printf("error number.\n") ;

            Display_Board (show , row , col) ;

        }

    }

    if (win == (row * col) - count)
    {

        printf("congratulations ! you win.\n") ;

        Display_Board (mine , row , col) ;

        Sleep(5000) ;

        system("cls") ;

    }

}

// 游戏流程

void Game_Play ()
{

    // 创建mine和show棋盘
    char mine [ROWS] [COLS] ;
    char show [ROWS] [COLS] ;

    // 初始化mine和show棋盘
    Init_Board (mine , ROWS , COLS , '0') ;
    Init_Board (show , ROWS , COLS , '*') ;

    // 测试打印mine和show棋盘
    // Display_Board (mine , ROW , COL) ;
    // Display_Board (show , ROW , COL) ;

    // 向mine棋盘布置雷
    Set_Mine (mine , ROW , COL , COUNT) ;
    // Display_Board (mine , ROW , COL) ;

    // 输入坐标扫雷
    Find_Mine (mine , show , ROW , COL , COUNT) ;

}